package com.mall.db.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.OmsOrderMapper;
import com.mall.domain.OmsOrder;
import com.mall.interface1.api.OrderApi;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class OrderApiImpl implements OrderApi {

    @Autowired
    private OmsOrderMapper omsOrderMapper;

    //根据订单id查询指定订单详情
    public OmsOrder findById(Long id) {
        return omsOrderMapper.selectById(id);
    }

    //根据订单id集合查询订单详情
    public List<OmsOrder> findByIds(List<Long> ids) {
        return omsOrderMapper.selectBatchIds(ids);
    }

    //分页查询订单详情
    public Page pageOrder(Integer pageNum, Integer pageSize, OmsOrder order) {
        Page page = new Page(pageNum, pageSize);
        QueryWrapper<OmsOrder> qw = new QueryWrapper<>();
//        if (order.getCreateTime() != null) {
//            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//            Date date = null;
//            try {
//                date = format.parse(order.getCreateTime());
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }
//            String startTime = DateUtils.getStartOfDay(date);
//            String endTime = DateUtils.getEndOfDay(date);
//            qw.between("create_time", startTime, endTime);
//        }
        qw.eq(StringUtils.isNotEmpty(order.getOrderSn()), "order_sn", order.getOrderSn()) //订单编号
                .eq(order.getOrderType() != null, "order_type", order.getOrderType())  //订单类型
                .eq(order.getSourceType() != null, "source_type", order.getSourceType())  //订单来源
                .eq("delete_status", 0)  //删除状态：0->未删除
                .eq(order.getStatus() != null, "status", order.getStatus())//订单状态
                .like(StringUtils.isNotEmpty(order.getReceiverName()), "receiver_name", order.getReceiverName())//收货人
                .eq(StringUtils.isNotEmpty(order.getReceiverPhone()), "receiver_phone", order.getReceiverPhone())//收货人手机号码
                .orderByDesc("create_time");
        return omsOrderMapper.selectPage(page, qw);
    }

    //修改订单费用信息
    public Integer updateOrder(OmsOrder order) {
        return omsOrderMapper.updateById(order);
    }

    //批量更新订单
    public Integer updateOrderList(List<OmsOrder> orderList) {
        Integer count = null;
        for (OmsOrder order : orderList) {
            count = omsOrderMapper.updateById(order);
        }
        return count;
    }
}
